var userid = null;
var password = null;
var commitForm = null;
function isBlank(str){if(str==null||str=="")return true;return false;}
function keyDown(){
	if(userid && password && submitBtn){
		if(event.keyCode == 13){
			if(!isBlank(userid.getRawValue()) && !isBlank(password.getRawValue())){
				commitForm();
			}
		}
	}
}

Ext.onReady(function() {
	
	var waitingmsg='<div><img style="margin-right:3px;float:left;vertical-align:top;" src="' + ctx + '/common/images/loading.gif">&nbsp;' + str_login_waiting + '</div>';
	
	var waitingLabel = Ext.create('Ext.form.Label', {
		html:waitingmsg,
		hidden: true
	});
	
	var msgLabel = Ext.create('Ext.form.Label', {
		text:''
	});
	
	var msgContainer = Ext.create('Ext.container.Container',{
		items:[waitingLabel,msgLabel]
	});
	
	userid = Ext.create('Ext.form.field.Text',{
        fieldLabel: str_login_userid,
        name: 'usr',
        maxLength : 20,
        minLength : 1,
        allowBlank : false,
        anchor:'100%'
	});
	
	password = Ext.create('Ext.form.field.Text',{
		fieldLabel: str_lang_pwd,
        name: 'pwd',
        inputType : 'password',
        maxLength : 12,
        minLength : 1,
        allowBlank : false,
        anchor: '100%'
	});
	
    var form = Ext.create('Ext.form.Panel', {
        fieldDefaults: {
            labelWidth: 55,
            labelAlign: 'top',
            msgTarget: 'under'
        },
        defaultType: 'textfield',
        bodyPadding: 5,
        items: [userid,password,{
            xtype: 'hiddenfield',
            name: 'md',
            value: 'dl'
        }, msgContainer]
    });
    submitBtn = Ext.create('Ext.button.Button', {
        text: str_login_login,
        listeners: {
        	click : {
        		fn : cmtFormBtn
        	}
        }
    });
    
    commitForm = function(){
    	if(submitBtn.isDisabled()){
    		return;
    	}
    	submitBtn.setDisabled(true);
    	waitingLabel.show();
    	msgLabel.setText('');
    	form.getForm().submit({
    		clientValidation: true,
    		method : 'POST',
    		url: ctx + '/aa/po/login.do',
    		success: function(form, action) {
    			msgLabel.setText(action.result.msg);
    			waitingLabel.hide();
    			window.location.href = ctx;
    		},
    		failure: function(form, action) {
    			submitBtn.setDisabled(false);
    			msgLabel.setText(action.result.msg);
    			waitingLabel.hide();
    			userid.focus(true);
    		}
    	});
    };
    
    function cmtFormBtn(){
    	if(!isBlank(userid.getRawValue()) && !isBlank(password.getRawValue())){
			commitForm();
		}
    }

    var win = Ext.create('Ext.window.Window', {
        title: str_login_login,
        closable   : false,
        collapsible: true,
        width: 300,
        height:210,
        minWidth: 300,
        minHeight: 100,
        layout: 'fit',
        plain: true,
        items: form,
        buttons: [submitBtn,{
            text: str_login_reset,
            handler : function() {
            	form.getForm().reset();
            	msgLabel.setText('');
            	waitingLabel.hide();
            }
        }]
    });

    win.show();
});